package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.User;
import com.wms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czh
 * @since 2025-06-04
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

@GetMapping("/list")
    public List<User> list(){
    return userService.list();
}
@PostMapping("/save")
    public Result save(@RequestBody User user){

    userService.save(user);
    return Result.success();
}
@PostMapping("/mod")
    public Result mod(@RequestBody User user){
    userService.updateById(user);
    return Result.success();
}
@PostMapping("/saveormod")
    public boolean saveormod(@RequestBody User user){
    return userService.saveOrUpdate(user);
}
@PostMapping("/delete")
    public Result delete(@RequestBody User user){
    userService.removeById(user.getId());
    return Result.success();
}
//登录
@PostMapping("/login")
public Result login(@RequestBody User user) {
    String no = user.getNo();
    String password = user.getPassword();
    List<User> userList = userService.list();

    for (User u : userList) {
        if (no.equals(u.getNo()) && password.equals(u.getPassword())) {
            // 登录成功，返回用户信息
            return Result.success(u);
        }
    }
    return Result.fail();
}



//模糊查询
    @PostMapping("/listL")
    public Result listP(@RequestBody User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
    if(StringUtils.isNotBlank(user.getName())){

        lambdaQueryWrapper.like(User::getName, user.getName());
    }

        return Result.success(userService.list(lambdaQueryWrapper));
    }
//分页查询
    @PostMapping("/listPage")
        public List<User> listPage(@RequestBody QueryPageParam queryPageParam) {
            HashMap param =queryPageParam.getParam();
            String name = (String) param.get("name");
        Page<User> page = new Page(queryPageParam.getPageNum(), queryPageParam.getPageSize());
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getName,name);
        IPage result = userService.page(page, lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        return result.getRecords();
    }
    //自定义结果集查询
    @PostMapping("/listResult")
    public Result listResult(@RequestBody QueryPageParam queryPageParam) {
        HashMap param =queryPageParam.getParam();
        String name = (String) param.get("name");
        String sex=(String) param.get("sex");
        String roleId=(String) param.get("roleId");
        Page<User> page = new Page(queryPageParam.getPageNum(), queryPageParam.getPageSize());
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name)&&!"null".equals(name)){
            lambdaQueryWrapper.like(User::getName,name);
        }
        if(StringUtils.isNotBlank(sex)){
            lambdaQueryWrapper.like(User::getSex,sex);
        }
        if(StringUtils.isNotBlank(roleId)){
            lambdaQueryWrapper.like(User::getRoleId,roleId);
        }
        IPage result = userService.page(page, lambdaQueryWrapper);
        return Result.success(result.getRecords(),result.getTotal());
    }
    @GetMapping("/findByNo")
    public Result findByNo(String no) {
        List<User> list = userService.lambdaQuery()
                .eq(User::getNo, no)
                .list();

        if (list.size() > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

}
